package com.example.entity.delay;

import lombok.Data;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * @author ChenXiaoBin
 * on 2019/6/17
 */
@Data
public class Message implements Delayed{

    private int id;
    /**
     * 消息内容
      */
    private String body;


    private String name;


    private Integer sex;

    /**
     * 延迟时长，这个是必须的属性因为要按照这个判断延时时长
     */
    private long excuteTime;

    // 延迟任务是否到时就是按照这个方法判断如果返回的是负数则说明到期否则还没到期
    @Override
    public long getDelay(TimeUnit unit) {
        return unit.convert(this.excuteTime - System.nanoTime(), TimeUnit.NANOSECONDS);
    }

    // 自定义实现比较方法返回 1 0 -1三个参数
    @Override
    public int compareTo(Delayed delayed) {
        return 0;
    }

    public Message(int id, String body, String name, Integer sex,  long delayTime) {
        this.id = id;
        this.body = body;
        this.name = name;
        this.sex = sex;
        this.excuteTime = TimeUnit.NANOSECONDS.convert(delayTime, TimeUnit.MILLISECONDS) + System.nanoTime();

    }


}
